package com.distributed.common.message;

import com.distributed.common.Constant;

import net.sf.json.JSONObject;

public class AskMessage extends BaseMessage {
	
	public AskMessage() {
		super();
		setId(Constant.ASK_MESSAGE);
	}

	public AskMessage(String target, String content, String status) {
		super();
		setId(Constant.ASK_MESSAGE);
		setTarget(target);
		setContent(content);
		setStat(status);
	}

	public static String getName() {
		return Constant.ASK_MESSAGE_NAME;
	}

	public void setTarget(String target) {
		argv.put(Constant.TARGET, target);
	}

	public String getTarget() {
		return argv.get(Constant.TARGET);
	}

	public void setContent(String content) {
		argv.put(Constant.CONTENT, content);
	}

	public String getContent() {
		return argv.get(Constant.CONTENT);
	}

	public void setStat(String status) {
		argv.put(Constant.STATUS, status);
	}

	public String getStat() {
		return argv.get(Constant.STATUS);
	}

	public static AskMessage getFromString(String message) {
		JSONObject jsonobject = JSONObject.fromObject(message);
		if (jsonobject == null) {
			return null;
		}
		return getFromJSON(jsonobject);
	}

	public static AskMessage getFromJSON(JSONObject jsonobj) {
		BaseMessage baseMessage = new AskMessage();
		BaseMessage baseMsg = BaseMessage.getFromJSON(jsonobj, baseMessage);
		
		if (baseMsg == null) {
			return null;
		}
		if (baseMsg.getId() != Constant.ASK_MESSAGE) {
			return null;
		}
		String target = baseMsg.getArgvValue(Constant.TARGET);
		if (target == null || "".equals(target)) {
			return null;
		}
		String content = baseMsg.getArgvValue(Constant.CONTENT);
		if (content == null || "".equals(content)) {
			return null;
		}
		String status = baseMsg.getArgvValue(Constant.STATUS);
		if (status == null || "".equals(status)) {
			return null;
		}
		
		return (AskMessage)baseMsg;
	}

	public JSONObject toJSON() {
		return super.toJSON();
	}
	
	public String toString() {
		return toJSON().toString();
	}
}
